// ParaSite: chat related class definitions


// Chat container and objectifier...
function Para_Chat( ) {
    this.known_ids   = new Array();
    this.newest_id   = 0;
    this.shout_count = 0;
    this.html        = '';

    this.console = $('para_chat_console');

    // methods 
    this.update          = Para_Chat_Update;
    this.init            = Para_Chat_Init;
    this.clear           = Para_Chat_Clear;
    this.rpc_response    = Para_Chat_RPC_Response;
    this.handle_events   = Para_Chat_Handle_Events;
    this.fetch_chat      = Para_Chat_Fetch_Chat;

    // Server Events //
    this.sev_msglist = Para_Chat_SEV_MsgList;
    this.sev_postmsg = Para_Chat_SEV_PostMsg;

    // User events
    this.uev_chat_post = Para_Chat_UEV_ChatPost;
}


function Para_Chat_Clear() {
    this.known_ids    = new Array();
    this.shout_count  = 0;
    this.newest_id    = 0;
    this.html         = '';
}

function Para_Chat_RPC_Response( response ) {
    var postback = response.postback;

    switch (postback) {
    case "chat_msglist":
        this.sev_msglist( response ); break;
    case "chat_post_msg":
        this.sev_postmsg( response ); break;
    default:
        alert( "Unknown postback:" + postback );
    }
}

function Para_Chat_Handle_Events( events ) {

    var reload_chat = false;
    for(var i = 0; i < events.length; i++) {
        var cur = events[i];
        var section = cur.section;
        var name    = cur.name;

        if( section  == "CHAT" )
            reload_chat = true;

    }
     
    if( reload_chat )
        this.fetch_chat( );

}


function Para_Chat_SEV_MsgList( response  ) {
    var status = response.status;
    if(status == "ok") {
        var html         = response.retval.html;
        var shout_count  = response.retval.shout_count; 
        var newest_id    = response.retval.newest_id;

        this.update( html, shout_count, newest_id );
    }
    else {
        alert("Para_Forum_SEV_Msglist failed");
    }
}


function Para_Chat_SEV_PostMsg( response ) {
    var status = response.status;
    if(status == "ok") {
        var html         = response.retval.html;
        var shout_count  = response.retval.shout_count;
        var newest_id    = response.retval.newest_id;

        this.update( html, shout_count, newest_id );
    }
    else {
        parapara.status_error("ParaPara_SEV_PostMsg failed: " + response.error_id );
    }
}


function Para_Chat_UEV_ChatPost( ) {
    var field = $('para_chatform_text');
    var text = field.get('value');
    field.value = '';

    if(!text || !text.length)
        return;

    var request = 
        {
            operation: "chat_post_msg",
            postback:  "chat_post_msg",
            param: {
                text: text
            }
        };
    parapara.send_rpc( request );

}


// update state from the server json response
function Para_Chat_Update( html, shout_count, newest_id ) {

    if(!this.console)
        return;

    if(newest_id == this.newest_id)
        return;

    this.console.empty();
    this.console.set( 'html', html );
    this.shout_count = shout_count;

    // find shout elements inside the para_shout_shouts element and take their ids
    var elements = $$('#para_chat_shouts div.para_chat_shout');
    var cur_ids  = new Array();
    if( elements != null )
        for(var i = 0; i < elements.length; i++) {
            var id = elements[i].id;
            cur_ids.push( id );
            if( !this.known_ids.contains(id) ) {
                //var fx = new Fx.Style(elements[i], 'background-color', {duration:2000});
                //FIXME: remove hard coded colors
                //fx.start('#000000', '#ffffff');
            }
        }

    this.known_ids = cur_ids;
    this.newest_id = newest_id;
}

function Para_Chat_Fetch_Chat( ) {
    var postback = "chat_msglist";

    var request = 
        {
            operation: "chat_msglist",
            postback:  postback,
            param: {}
        };
   parapara.send_rpc( request );
}



function Para_Chat_Init() {
    // find form and override the submit event
    var form = $('para_chatform');
    form.set({ events: {"submit": Para_Chat_UEV_Submit } });

    // Construct array of known message ids for use with smart fading effects
    var elements = $$('#para_chat_shouts div.para_chat_shout');
    if( elements != null )
        for(var i = 0; i < elements.length; i++) {
            var id = elements[i].id;
            this.known_ids.push(id);
        }

    this.fetch_chat( );
}



function Para_Chat_UEV_Submit( event ) {
    var e = new Event(event);
    e.stop();    
    parapara.chat.uev_chat_post();

}

function Para_Chat_UEV_KeyUp( event ) {
    var code = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (code == 13) {
        parapara.chat.uev_chat_post();
        return false;
    } 
    else return true;
}
